package com.persagy.energy.app

import com.persagy.energy.bean.EnergyBean
import com.persagy.energy.utils.DTUtils
import net.minidev.json.JSONObject
import net.minidev.json.parser.JSONParser

import java.io.{File, FileWriter, PrintWriter}
import java.lang.reflect.Field
import scala.io.BufferedSource

object File2FileApp {

  val filePath: String = "/Users/king/Downloads/HbaseCat-2.2.1/fjd_0_near_1d_2020.json"
  val outPath: String = "/Users/king/Downloads/fjd_0_near_1d_2020.txt"

  def main(args: Array[String]): Unit = {

    /** 从文件读取数据，逐条发送 */
    val bufferedSource: BufferedSource = scala.io.Source.fromFile(filePath)
    var count: Int = 0
    val writer = new PrintWriter(outPath)
    for (line <- bufferedSource.getLines()) {
      count += 1
      println(line)
      val parser = new JSONParser(1)
      val nObject: JSONObject = parser.parse(line).asInstanceOf[JSONObject]

//      val energyModelSign: String = nObject.get("energyModelSign").toString
//      val energyModelNodeSign: String = nObject.get("energyModelNodeSign").toString
//      val dataTime: String = DTUtils.timestampConverter("yyyyMMddHHmmss", nObject.get("data_time").toString, 0L).toString
//      val dataValue: String = nObject.get("data_value").toString
//      val building: String = nObject.get("building").toString
//
//      val result: String = energyModelSign + "\t" + energyModelNodeSign + "\t" + dataTime + "\t" + dataValue + "\t" + building

      val building: String = nObject.get("building").toString
      val meter: String = nObject.get("meter").toString
      val funcId: String = nObject.get("funcid").toString
      val dataTime: String = DTUtils.timestampConverter("yyyyMMddHHmmss", nObject.get("data_time").toString, 0L).toString
      val dataFlag: String = nObject.get("data_flag").toString
      val dataValue: String = nObject.get("data_value").toString

      val result: String = building + "\t" + meter + "\t" + funcId + "\t" + dataTime + "\t" + dataFlag + "\t" + dataValue

      writer.println(result)

      if (count == 5000){
        writer.flush()
      }
    }
    writer.flush()
    writer.close()
  }
}
